﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WillowTree
{
    public partial class SlideSelector : UserControl
    {
        public delegate string SlideIndexTranslator(object obj);

        public event EventHandler ValueChanged;

        public event SlideIndexTranslator IndexTranslator;
 
        public virtual void OnValueChanged(EventArgs e)
        {
            if (this.IndexTranslator != null)
                this.SliderLabel.Text = this.IndexTranslator(this);

            if (this.ValueChanged != null)
                this.ValueChanged(this, e);
        }

        private void Slider_ValueChanged(object obj, EventArgs e)
        {
            if (this.InputMode == WillowTree.InputMode.Standard)
            {
                this.OnValueChanged(e);
            }
        }

        private void UpDown_ValueChanged(object obj, EventArgs e)
        {
            if (this.InputMode == WillowTree.InputMode.Advanced)
            {
                this.OnValueChanged(e);
            }
        }

        void RaiseOnValueChanged()
        {
            this.OnValueChanged(EventArgs.Empty);
        }

        public WillowTree.InputMode _InputMethod;
        public WillowTree.InputMode InputMode
        {
            get { return this._InputMethod; }
            set
            {
                if (_InputMethod != value)
                    SetInputMethod(value, this.UpDown.Hexadecimal);
            }
        }

        public int Maximum
        {
            set { this.Slider.Maximum = value; }
            get { return this.Slider.Maximum; }
        }
        public int Minimum
        {
            set { this.Slider.Minimum = value; }
            get { return this.Slider.Minimum; }
        }

        public int MaximumAdvanced
        {
            set { this.UpDown.Maximum = value; }
            get { return (int)this.UpDown.Maximum; }
        }

        public int MinimumAdvanced
        {
            set { this.UpDown.Minimum = value; }
            get { return (int)this.UpDown.Minimum; }
        }

        public string Caption
        {
            set { this.SliderLabel.Text = value; }
            get { return this.SliderLabel.Text; }
        }

        private Int16 ConvertFromUpDown(Decimal value)
        {   // A series of type casts to turn a value from 0 to 65535 in a
            // numeric up/down control into an int16 value of -32768 to 32767
            return (short)((ushort)((uint)value));
        }

        private Decimal ConvertToUpDown(int value)
        {   // A series of type casts to turn an int value of -32768 to 32767
            // into a decimal value of 0 to 65535 for a numeric up/down control
            return (decimal)((ushort)((short)value));
        }

        public virtual int Value
        {
            set
            {
                if (this.InputMode == WillowTree.InputMode.Standard)
                {
                    int SValue = value;

                    if (SValue > this.Slider.Maximum)
                        this.Slider.Value = this.Slider.Maximum;
                    else if (SValue < Slider.Minimum)
                        this.Slider.Value = this.Slider.Minimum;
                    else
                        this.Slider.Value = SValue;
                }
                else
                    this.UpDown.Value = ConvertToUpDown(value);
                OnValueChanged(EventArgs.Empty);
            }
            get
            {
                if (this.InputMode == WillowTree.InputMode.Standard)
                    return this.Slider.Value;
                else
                    return ConvertFromUpDown(this.UpDown.Value);
            }
        }

        public void SetInputMethod(WillowTree.InputMode method, bool UseHex)
        {
            this.UpDown.Hexadecimal = UseHex;
            if (method == _InputMethod)
                return;

            _InputMethod = method;
            if (method == WillowTree.InputMode.Standard)
            {
                int SValue = ConvertFromUpDown(this.UpDown.Value);

                if (SValue > this.Slider.Maximum)
                    this.Slider.Value = this.Slider.Maximum;
                else if (SValue < Slider.Minimum)
                    this.Slider.Value = this.Slider.Minimum;
                else                
                    this.Slider.Value = SValue;

                Slider_ValueChanged(this, EventArgs.Empty);
                this.UpDown.Hide();
                this.Slider.Show();
            }
            else
            {
                this.UpDown.Value = ConvertToUpDown(this.Slider.Value);
                UpDown_ValueChanged(this, EventArgs.Empty);
                this.Slider.Hide();
                this.UpDown.Show();
            }
            this._InputMethod = method;
        }

        public SlideSelector()
        {
            InitializeComponent();

            this.SetInputMethod(GlobalSettings.InputMode, this.UpDown.Hexadecimal);
            if (this.InputMode == InputMode.Standard)
                UpDown.Hide();
            else
                Slider.Hide();
            GlobalSettings.InputMethodChanged += new GlobalSettings.InputMethodChangedEventHandler(SetInputMethod);
        }
    }
}